package com.lfg.leetcode.leetcode200;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class No228summaryRanges {

    public static void main(String[] args) {
        List<String> ret = summaryRanges(new int[]{1, 3, 4, 10, 11, 12, 13, 16});
        System.out.println(ret);
    }

    public static List<String> summaryRanges(int[] nums) {
        List<String> ret = new ArrayList<>(nums.length);
        StringBuilder data = new StringBuilder(nums.length);

        Integer start = null;
        Integer last = null;
        for (int i : nums) {
            if (last == null) {
                last = i;
                start = i;
            } else if (last + 1 != i) {
                data.append(start);
                if (!Objects.equals(start, last)) {
                    data.append("->").append(last);
                }
                start = i;
                last = i;
                ret.add(data.toString());
                data.delete(0, data.length());
            } else {
                last = i;
            }
        }

        if (null != last) {
            data.append(start);
            if (!Objects.equals(start, last)) {
                data.append("->").append(last);
            }
            ret.add(data.toString());
            data.delete(0, data.length());
        }

        return ret;
    }


}
